<?php

namespace gzapi\common\business;
use gzapi\common\model\GzHiseServiceProjectModel;
use gzapi\common\model\GzHiseServiceInspectionModel;
use gzapi\common\model\GzHiseFollowModel;
use gzapi\common\model\RlCompanyModel;
use gzapi\common\model\RlZsmsUserQueryModel;
use  gzapi\common\model\GzHiseServiceProTestModel;
class Department
{
    protected $GzHiseServiceProject;
    protected $GzHiseServiceInspection;
    protected $GzHiseFollow;
    protected $RlCompanyModel;
    protected $RlZsmsUserQueryModel;
    protected $GzHiseServiceProTestModel;
    protected $classification = [
1 =>'公建',  2 =>'房地产'
        ];
    protected $pfautoid = [
16 =>'工厂',
17 =>'住宅',
18 =>'商铺',
19 =>'囤货',
21 =>'其他',
1 =>'医院',
2 =>'学校',
3 =>'电力系统',
4 =>'铁路系统',
5 =>'通信系统',
6 =>'样板房（间）',
7 =>'综合大楼',
8 =>'售楼处',
9 =>'餐饮娱乐',
15 =>'商场',
20 =>'银行',
24 =>'商业综合体'
        ];
    public function __construct()
    {
        $this->GzHiseServiceProject = new GzHiseServiceProjectModel;
        $this->GzHiseServiceInspection = new GzHiseServiceInspectionModel;
        $this->GzHiseFollow = new GzHiseFollowModel;
        $this->RlCompanyModel = new RlCompanyModel;
        $this->RlZsmsUserQueryModel = new RlZsmsUserQueryModel;
        $this->GzHiseServiceProTestModel = new GzHiseServiceProTestModel;
    }

    //获取未开工和安装中的数量
    public function azdNum($where = [])
    {
        $num = $this->GzHiseServiceProject->num($where);
        return $num;
    }

    //获取巡检进行中的安装单数量
    public function inspectListNumIng($where = [],$azdwhere=[])
    {

        $num = $this->GzHiseServiceInspection->inspectListNumIng($where,$azdwhere=[]);
        return $num;
    }

    //获取工程列表
    /*$type   未开工，安装，运行，保外
     *$follow  关注
     *
     * */
    public function projectList($where = [],$field = "*",$page = 0,$limit = 10,$inspectionWhere = [],$followWhere=[])
    {
        if(isset($where['xj_fwgcs']))
        {
            $inspectionWhere['xj_fwgcs'] = $where['xj_fwgcs'];
            unset($where['xj_fwgcs']);
        }
        //获取工程
        $data = $this->GzHiseServiceProject->list($where,$field,$page,$limit);
        //获取关注信息
        $datafl = $this->followList($followWhere);
        //获取工程的巡检数量
        $azd_id_arr = array_column($data, 'azd_id');
        $inspectionWhere['xj_azdbh'] = ['in', $azd_id_arr];
        $inspection_arr = $this->GzHiseServiceInspection->inspectNumCount($inspectionWhere);
        foreach ($data as $k=>&$v)
        {
            if(strstr($datafl['follow'], $v['azd_id']))
            {
                $v['follow'] = 1;
            }else{
                $v['follow'] = 0;
            }
            $v['inspection_num'] = 0;
            foreach ($inspection_arr as $key=>$val)
            {
                if($val['xj_azdbh'] == $v['azd_id'])
                {
                    $v['inspection_num'] = $val['xj_total'];
                }
            }
        }
        return $data;
    }

    //获取项目详情
    public function project($where)
    {
        $data = $this->GzHiseServiceProject->getOne($where);
        //替换code对应的信息
        $data['fgsmz'] = $this->RlCompanyModel->where('orgdept',$data['qyfgs'])->find();
        $data['fwfgs_mz'] = $this->RlCompanyModel->where('orgdept',$data['fwfgs'])->find();
        $data['is_fb'] = $data['is_fb']?'是':'否';
        $data['sfkq_bs'] = $data['sfkq_bs']?'是':'否';
        $data['classification'] = $this->classification[$data['classification']];
        if(array_key_exists($data['pfautoid'],$this->pfautoid)){
            $data['pfautoid'] = $this->pfautoid[$data['pfautoid']];   
        }else{
            $data['pfautoid'] = '未知';
        }
       
        return $data;
    }
    //获取关注的项目编号
    /*$whaer   关注表搜索条件
     * */
    public function followList($where)
    {
        $datas = $this->GzHiseFollow->list($where);
        $data['follow'] = implode(',',array_column($datas, 'azd_id'));
        return $data;
    }
    //获取关注的项目数量
    /*
     * */
    public function followCount($where)
    {
        $data['num'] = $this->GzHiseFollow->num($where);
        return $data;
    }

    //取消,关注
    /*$azd_id  安装单编号
     * $type  取消，关注
     * */
    public function followOffOn($type,$where,$data=[])
    {
        if($type==2)
        {
            $result = $this->GzHiseFollow->del($where);
        }else{
            $azd = $this->GzHiseServiceProject->getOne(['azd_id'=>$data['azd_id']]);
            if($azd['strategic_partner_code'] && $azd['strategic_partner_code'] != 0)
            {
                $data['is_strategic'] = 1;
            }
            $result = $this->GzHiseFollow->add($data);
        }
        return $result;
    }

    //安装单查询条件整理方法
    public function selectazd($data)
    {
        $where = [];
        //用户名字
        if (isset($data['yh_name']) && $data['yh_name']) {
            $where['yh_name'] = ['like', '%' . $data['yh_name'] . '%'];
        }
        $ins = [];
        //安装单编号
        if (isset($data['azd_id']) && $data['azd_id']) {
            $where['azd_id'] = ['like', '%' . $data['azd_id'] . '%'];
            $ins['xj_azdbh'] = ['like', '%' . $data['azd_id'] . '%'];
        }
        //用户手机号
        if (isset($data['sj_one']) && $data['sj_one']) {
            $where['sj_one'] = ['like', '%' . $data['sj_one'] . '%'];
        }
        //详细地址
        if (isset($data['address']) && $data['address']) {
            $where['address'] = ['like', '%' . $data['address'] . '%'];
        }
        //工程名称
        if (isset($data['pname']) && $data['pname']) {
            $where['pname'] = ['like', '%' . $data['pname'] . '%'];
        }
        //工程状态
        if (isset($data['gczt'])) {
            $where['gczt'] = ['in',$data['gczt']];
        }
        //工程状态
        if (isset($data['is_strategic']) && $data['is_strategic'] == 1) {
            $where['strategic_partner_code'] = ['<>','0'];
        }
        //服务工程师
        if (isset($data['service_engineer']) && $data['service_engineer']) {
            $where['service_engineer'] = ['like', '%' . $data['service_engineer'] . '%'];
        }
        //销售工程师
        if (isset($data['sales_engineer']) && $data['sales_engineer']) {
            $where['sales_engineer'] = ['like', '%' . $data['sales_engineer'] . '%'];
        }
        //巡检状态区分，获取本状态下的azd_id
        //xj_xjzt 1 没有巡检单  2 巡检中  3验证中   4巡检结束
        if (isset($data['xj_xjzt']) && $data['xj_xjzt']) {
            if(isset($data['mobilphone']))
            {
                $ins['xj_fwgcs'] = $data['mobilphone'];
            }
            if(isset($data['xj_dw']))
            {
                $ins['xj_dw'] = $data['xj_dw'];
            }
            if(isset($data['xj_xjr']))
            {
                $ins['xj_xjr'] = $data['xj_xjr'];
            }
            if($data['xj_xjzt'] == 1)
            {
                $where['inspection'] = 0;
            }else if($data['xj_xjzt'] == 2){
                $azd_ids = $this->GzHiseServiceInspection->where('xj_xjzt','in','0,1,2')->where($ins)->column('xj_azdbh');
                $where['azd_id'] = ['in', $azd_ids];
            } else if($data['xj_xjzt'] == 3){
                $azd_ids = $this->GzHiseServiceInspection->where('xj_xjzt','in','3,4')->where($ins)->column('xj_azdbh');
                $where['azd_id'] = ['in', $azd_ids];
            }else if($data['xj_xjzt'] == 4){
                $azd_ids = $this->GzHiseServiceInspection->where('xj_xjzt',5)->where($ins)->column('xj_azdbh');
                $where['azd_id'] = ['in',$azd_ids];
            }
        }
        return $where;
    }

    //服务商和服务人员接收巡检的安装单数量
    public function serviceAzdNum($where)
    {
        $data = $this->GzHiseServiceInspection->inspectAzdNum($where);
        return $data;
    }

    //获取分工司，服务人员巡检中的安装单编号
    public function inspectListstr($where)
    {
        $data = $this->GzHiseServiceInspection->azdIdArr($where);
        return $data;
    }

    //服务商和服务人员接收巡检的安装单列表
    public function serviceAzdList($where,$field = "*",$page = 0,$limit = 10,$followWhere)
    {
        $projectWhere = $where;
        //获取调试单中的azd_id
        $testwhere['ts_dw'] = $where['xj_dw'];
        if(isset($where['xj_xjr']))
        {
            $testwhere['ts_ry'] = $where['xj_xjr'];
        }
        if(isset($projectWhere['azd_id']))
        {
            $testwhere['azd_id'] = $projectWhere['azd_id'];
        }
        $testazdIdArr = $this->GzHiseServiceProTestModel->azdIdArr($testwhere);
        //获取巡检单中的azd_id
        $inspectwhere['xj_dw'] = $where['xj_dw'];
        $inspectionWhere['xj_dw'] = $where['xj_dw'];
        if(isset($where['xj_xjr']))
        {
            $inspectwhere['xj_xjr'] = $where['xj_xjr'];
            $inspectionWhere['xj_xjr'] = $where['xj_xjr'];
        }
        if(isset($projectWhere['azd_id']))
        {
            $inspectwhere['xj_azdbh'] = $projectWhere['azd_id'];
        }
        $azdIdArr = $this->GzHiseServiceInspection->azdIdArr($inspectwhere);
        $azdIdArr = array_merge($azdIdArr,$testazdIdArr);
        unset($projectWhere['xj_dw']);
        unset($projectWhere['xj_xjr']);
        if(isset($projectWhere['xj_xjzt']))
        {
            unset($projectWhere['xj_xjzt']);
        }
        $projectWhere['azd_id'] = ['in',$azdIdArr];
        $data = $this->GzHiseServiceProject->list($projectWhere,'*',$page,$limit);
        //获取关注信息
        $datafl = $this->followList($followWhere);
        //获取工程的巡检数量
        $azd_id_arr = array_column($data, 'azd_id');
        $inspectionWhere['xj_azdbh'] = ['in', $azd_id_arr];
        $inspection_arr = $this->GzHiseServiceInspection->inspectNumCount($inspectionWhere);
        foreach ($data as $k=>&$v)
        {
            if(strstr($datafl['follow'], $v['azd_id']))
            {
                $v['follow'] = 1;
            }else{
                $v['follow'] = 0;
            }
            $v['inspection_num'] = 0;
            foreach ($inspection_arr as $key=>$val)
            {
                if($val['xj_azdbh'] == $v['azd_id'])
                {
                    $v['inspection_num'] = $val['xj_total'];
                }
            }
        }
        return $data;
    }

    public function editazd($where,$data)
    {
        $res = $this->GzHiseServiceProject->where($where)->update($data);
        return $res;
    }

    //获取服务工程师
    public function getfwgcs($where)
    {
        $data = $this->RlZsmsUserQueryModel->where($where)->select()->toArray();
        return $data;
    }
}